package com.bincloud.vue.admin.web.service.ums.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bincloud.vue.admin.common.enums.ums.UmsRoleStateEnum;
import com.bincloud.vue.admin.common.exception.BaseException;
import com.bincloud.vue.admin.common.exception.CodeMsg;
import com.bincloud.vue.admin.common.model.dto.response.BasePageResDTO;
import com.bincloud.vue.admin.common.util.BaseEntityUtils;
import com.bincloud.vue.admin.common.util.ObjectUtils;
import com.bincloud.vue.admin.dao.mapper.ums.UmsRoleMapper;
import com.bincloud.vue.admin.model.entity.UmsRole;
import com.bincloud.vue.admin.web.dto.ums.role.*;
import com.bincloud.vue.admin.web.service.ums.IUmsRoleService;
import com.bincloud.vue.admin.web.util.request.RequestService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bincloud, mvpzhou
 * @since 2022-02-17
 */
@Service
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole> implements IUmsRoleService {
    @Resource
    private RequestService requestService;

    /**
     * 获取角色信息
     *
     * @param code 入参
     * @return 返参
     */
    @Override
    public UmsRole getRole(String code) {
        return this.getOne(Wrappers.<UmsRole>lambdaQuery().eq(UmsRole::getCode, code));
    }

    /**
     * 角色分页查询
     *
     * @param reqDTO 入参
     * @return 返参
     */
    @Override
    public BasePageResDTO<List<UmsRolePageResDTO>> pageRole(UmsRolePageReqDTO reqDTO) {
        // 分页查询入参
        QueryWrapper<UmsRole> queryWrapper = new QueryWrapper<>();
        BeanUtils.copyProperties(reqDTO, queryWrapper);
        Page<UmsRole> queryPage = new Page<>(reqDTO.getPageNo(), reqDTO.getPageSize());
        Page<UmsRole> resPage = this.page(queryPage, queryWrapper);
        // 组装参数
        List<UmsRolePageResDTO> resList = resPage.getRecords().stream().map(source -> {
            UmsRolePageResDTO target = new UmsRolePageResDTO();
            BeanUtils.copyProperties(source, target);
            return target;
        }).collect(Collectors.toList());
        // 组装返参
        BasePageResDTO<List<UmsRolePageResDTO>> resDTO = new BasePageResDTO<>();
        resDTO.setPageNo(resPage.getCurrent());
        resDTO.setPageSize(resPage.getSize());
        resDTO.setTotalPage(resPage.getPages());
        resDTO.setTotal(resPage.getTotal());
        resDTO.setContent(resList);
        return resDTO;
    }

    /**
     * 角色新增
     *
     * @param reqDTO 入参
     * @return 返参
     */
    @Override
    public boolean saveRole(UmsRoleSaveReqDTO reqDTO) {
        // 验证入参
        this.validateUmsRoleSaveReqDTO(reqDTO);
        // 组装参数
        UmsRole role = new UmsRole();
        BeanUtils.copyProperties(reqDTO, role);
        BaseEntityUtils.create(role, requestService.getUsername());
        return this.save(role);
    }

    /**
     * 角色删除
     *
     * @param reqDTO 入参
     * @return 返参
     */
    @Override
    public boolean removeRole(UmsRoleReqDTO reqDTO) {
        UmsRole role = this.getById(reqDTO.getId());
        if (ObjectUtils.isEmpty(role)) {
            throw new BaseException(CodeMsg.UMS_ROLE_NOT_EXIST);
        }
        // 删除数据
        return this.removeById(role);
    }

    /**
     * 角色修改
     *
     * @param reqDTO 入参
     * @return 返参
     */
    @Override
    public boolean modifyRole(UmsRoleModifyReqDTO reqDTO) {
        UmsRole role = this.getById(reqDTO.getId());
        if (ObjectUtils.isEmpty(role)) {
            throw new BaseException(CodeMsg.UMS_ROLE_NOT_EXIST);
        }
        this.validateUmsRoleModifyReqDTO(role, reqDTO);
        BeanUtils.copyProperties(reqDTO, role);
        return this.updateById(role);
    }

    private void validateUmsRoleModifyReqDTO(UmsRole role, UmsRoleModifyReqDTO reqDTO) {
        // 角色是否重复
        if (ObjectUtils.isNotEmpty(role) && ObjectUtils.equals(role.getId(), reqDTO.getId())) {
            throw new BaseException(CodeMsg.UMS_ROLE_EXISTED);
        }
        // 验证状态
        if (ObjectUtils.isEmpty(reqDTO.getState()) || UmsRoleStateEnum.noCode(reqDTO.getState())) {
            throw new BaseException(CodeMsg.UMS_ROLE_STATE_ERROR);
        }
    }

    private void validateUmsRoleSaveReqDTO(UmsRoleSaveReqDTO reqDTO) {
        // 角色是否重复
        UmsRole role = this.getRole(reqDTO.getCode());
        if (ObjectUtils.isNotEmpty(role)) {
            throw new BaseException(CodeMsg.UMS_ROLE_EXISTED);
        }
        // 验证状态
        if (ObjectUtils.isEmpty(reqDTO.getState()) || UmsRoleStateEnum.noCode(reqDTO.getState())) {
            throw new BaseException(CodeMsg.UMS_ROLE_STATE_ERROR);
        }
    }
}
